import { Tabs } from 'antd';
export function isRealArray(arr: any) {
    return Object.prototype.toString.call(arr) === '[object Array]';
}


export function maybeArray(arr: any) {
    // TODO
}


/**
 * 以命名空间的方式创建一个对象
 *
 * e.g:
 *  let obj = {};
 *  setNamespaceValue(obj , ['a','b','c'] , 123);
 *  console.log(obj.a.b.c); // => 123
 */
export function setNamespaceValue(obj: any, paths: string[] | string, value: any) {
    let name: string,
        temp = obj,
        i = 0;
    if (typeof paths === 'string') {
        paths = paths.split('.');
    }
    if (paths.length <= 0) {
        return;
    }
    for (; i < (paths.length - 1); i++) {
        name = paths[i];
        if (temp[name] && typeof temp[name] == 'object') {
            temp = temp[name];
        } else {
            temp = temp[name] = {};
        }
    }
    temp[paths[i]] = value;
}


/**
 * range 区间：
 *  [start - end]
 */
export function range(start: number, end: number) {
    const result = [];
    for (let i = start; i <= end; i++) {
        result.push(i);
    }
    return result;
}

/**
 * 删除一个命名空间
 */
export function deleteNamespace(obj: any, paths: string[]) {
    let name: string, temp = obj;
    if (!paths.length) {
        return false;
    }
    name = paths[paths.length - 1];
    for (let i = 0; i < paths.length - 1; i++) {
        try {
            temp = temp[paths[i]];
        } catch (e) {
            return false;
        }
    }
    try {
        return delete temp[name];
    } catch (e) {
        return false;
    }
}


/**
 * 简单判断一个key是否可以作为一个命名空间
 *
 * e.g: likeNamespaceKey('a.b.c'); //=> true
 * 默认仅简单判断
 * e.g: likeNamespaceKey('a.b.&*……') //=> true
 *
 * @export
 * @param {string} key
 * @returns
 */
export function likeNamespaceKey(namespace: string) {
    return namespace.indexOf('.') != -1;
}


/**
 * 从命名空间中获取一个值
 *
 * @export
 * @param {*} obj
 * @param {string[]} paths
 * @returns
 */
export function getNamespaceValue(obj: any, paths: string[] | string) {
    let name: string, temp = obj;
    if (typeof paths === 'string') {
        paths = paths.split('.');
    }
    for (let i = 0; i < paths.length; i++) {
        try {
            let key = paths[i];
            if (key && key.length) {
                temp = temp[paths[i]];
            }
        } catch (e) {
            return undefined;
        }
    }
    return temp;
}



/**
 * 设置Element的style并加上前缀
 *
 * @export
 * @param {HTMLElement} element
 * @param {React.CSSProperties} style
 */
const prefixs = ['ms', 'o', 'moz', 'webkit'];
export function setElementStyle(element: HTMLElement, style: React.CSSProperties) {
    Object.keys(style).forEach(attr => {

        let value = style[attr];
        element.style[attr] = value;

        // 将属性首字符大写 e.g: transform => Transform
        attr = attr.replace(/^\w/, (a: string) => a.toUpperCase());

        prefixs.forEach(prefix => {
            element.style[`${prefix}${attr}`] = value;
        });
    });
}



/**
 *
 * 查询字符串转换未一个对象
 * e.g:
 * xxx=999&yyy=777 => { xxx: 999 , yyy: 777 }
 * xxx[]=999&xxx[]=8888 => { xxx: [999,8888] }
 *
 */
export function queryStringToObject(query: string) {
    return query.split('&').reduce(function (result, v) {
        let [key, value] = v.split('=');
        if (key.indexOf('[]') === (key.length - 2)) {
            let arr: any[] = result[key.slice(0, -2)] || [];
            arr.push(value);
        } else {
            result[key] = value;
        }
        return result;
    }, {}) as any;
}

/**
 *
 * 得到当前URL的 Query Params
 */
export function getQueryObject() {
    let hash = window.location.hash;
    let queryString = hash.split('?')[1];
    if (queryString !== undefined) {
        return queryStringToObject(queryString);
    }
    return {};
}




/**
 * 复制一个对象，excludes 指定的字段将不被复制
 *
 * 因为后端返回的数据包含一些冗余数据，例如:id,createTime等。
 *
 * 注意:该实现为浅拷贝
 * e.g:
 *  copyParamsObject({a:'b',b: 'b'},['a'])// => {b: 'b'}
 */
export function copyParamsObject<T>(value: T, excludes: string[]): T {
    let result: any = {};
    return Object.getOwnPropertyNames(value).reduce(function (result, key) {
        if (excludes.indexOf(key) === -1) {
            result[key] = value[key];
        }
        return result;
    }, result);
}

export function copyParamsObjectWrapper(excludes: string[]) {
    return function <T>(value: T) {
        return copyParamsObject<T>(value, excludes);
    }
}
